<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Emit callbacks</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="yaml.callbacks.parse.html">« Parse callbacks</a></li>
      <li style="float: right;"><a href="ref.yaml.html">Yaml 函数 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="yaml.callbacks.html">Callbacks</a></li>
    <li>Emit callbacks</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="yaml.callbacks.emit" class="section">
  <h2 class="title">Emit callbacks</h2>
  <p class="para">
   Emit callbacks are invoked when an instance of a registered class is
   emitted by <span class="function"><a href="function.yaml-emit.html" class="function">yaml_emit()</a></span> or
   <span class="function"><a href="function.yaml-emit-file.html" class="function">yaml_emit_file()</a></span>. The callback is passed the object to
   be emitted. The callback must return an array having two keys:
   &quot;<code class="literal">tag</code>&quot; and &quot;<code class="literal">data</code>&quot;.
   The value associated with the &quot;<code class="literal">tag</code>&quot; key must
   be a string to be used as the YAML tag in the output. The value associated
   with the &quot;<code class="literal">data</code>&quot; key will be encoded as YAML
   and emitted in place of the intercepted object.
  </p>
  <div class="example" id="example-3855">
   <p><strong>示例 #1 Emit callback example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">EmitExample&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;data&nbsp;may&nbsp;be&nbsp;in&nbsp;any&nbsp;pecl/yaml&nbsp;suitable&nbsp;type<br /><br />&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$d</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;</span><span style="color: #FF8000">/**<br />&nbsp;&nbsp;&nbsp;*&nbsp;Yaml&nbsp;emit&nbsp;callback&nbsp;function,&nbsp;referred&nbsp;on&nbsp;yaml_emit&nbsp;call&nbsp;by&nbsp;class&nbsp;name.<br />&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;*&nbsp;Expected&nbsp;to&nbsp;return&nbsp;an&nbsp;array&nbsp;with&nbsp;2&nbsp;values:<br />&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;-&nbsp;'tag':&nbsp;custom&nbsp;tag&nbsp;for&nbsp;this&nbsp;serialization<br />&nbsp;&nbsp;&nbsp;*&nbsp;&nbsp;&nbsp;-&nbsp;'data':&nbsp;value&nbsp;to&nbsp;convert&nbsp;to&nbsp;yaml&nbsp;(array,&nbsp;string,&nbsp;bool,&nbsp;number)<br />&nbsp;&nbsp;&nbsp;*<br />&nbsp;&nbsp;&nbsp;*&nbsp;@param&nbsp;object&nbsp;$obj&nbsp;Object&nbsp;to&nbsp;be&nbsp;emitted<br />&nbsp;&nbsp;&nbsp;*&nbsp;@return&nbsp;array&nbsp;Tag&nbsp;and&nbsp;surrogate&nbsp;data&nbsp;to&nbsp;emit<br />&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;</span><span style="color: #007700">public&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">yamlEmit&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">EmitExample&nbsp;$obj</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'tag'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'!example/emit'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'data'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">data</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;);<br />&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$emit_callbacks&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'EmitExample'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #DD0000">'EmitExample'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yamlEmit'</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$t&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EmitExample</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">,</span><span style="color: #DD0000">'b'</span><span style="color: #007700">,</span><span style="color: #DD0000">'c'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$yaml&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">yaml_emit</span><span style="color: #007700">(<br />&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'example'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$t</span><span style="color: #007700">,<br />&nbsp;&nbsp;),<br />&nbsp;&nbsp;</span><span style="color: #0000BB">YAML_ANY_ENCODING</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #0000BB">YAML_ANY_BREAK</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$emit_callbacks<br /></span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$yaml</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
string(43) &quot;---
example: !example/emit
- a
- b
- c
...
&quot;
</pre></div>
   </div>
  </div>
 </div></div></div></body></html>